package com.example.musicplayer.model;
import android.media.*;

import com.example.musicplayer.myenum.EnumPlayMode;
import com.example.musicplayer.myinterface.ICompletionObserver;
import com.example.musicplayer.myinterface.IObservable;
import com.example.musicplayer.myinterface.IViewObserver;

import java.util.*;


public class Music extends Media implements IObservable
{

	private ArrayList<IViewObserver> viewObservers=new ArrayList();//视图观察者列表
	private ArrayList<ICompletionObserver> controllerObservers=new ArrayList();//播放完成观察者列表
	private String musicName;//音乐名
	private String singer;//歌手名
	private EnumPlayMode mode;//播放模式
	private MusicLrc musicLrc;//歌词对象
	private static Music music;//Music对象
	
	@Override
	public void registerViewObserver(IViewObserver observer)
	{
		viewObservers.add(observer);
	}

	@Override
	public void removeViewObserver(IViewObserver observer)
	{
		viewObservers.remove(observer);
	}

	@Override
	public void notifyViewObservers()
	{
		for(IViewObserver o:viewObservers){
			o.update();
		}
	}

	@Override
	public void registerCompletionObserver(ICompletionObserver observer)
	{
		controllerObservers.add(observer);
	}

	@Override
	public void removeCompletionObserver(ICompletionObserver observer)
	{
		controllerObservers.remove(observer);
	}

	@Override
	public void notifyCompletionObservers()
	{
		for(ICompletionObserver o:controllerObservers){
			o.updateCompletion();
		}
	}
	
	
	/**
	 * 初始化参数
	 */
	private Music(){
		
		mode=EnumPlayMode.MODE_ORDER;
		musicLrc=new MusicLrc();
		completion();
		new NotifyThread().start();
	}
	
	/**
	 * 采用单一模式获取对象
	 * @return Music对象
	 */
	public static synchronized Music getInstance(){
		if(music==null){
			music=new Music();
		}
		return music;
	}

	/**
	 * 设置播放完成监听
	 */
	public void completion(){
		this.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){

				@Override
				public void onCompletion(MediaPlayer p1)
				{
					notifyCompletionObservers();
				}
			});
	}
	
	//设置模式
	public void setMode(EnumPlayMode mode)
	{
		this.mode = mode;
	}

	//获取模式
	public EnumPlayMode getMode()
	{
		return mode;
	}

	//设置路径
	public void setPath(String path)
	{
		try
		{
			super.setPath(path);
			musicLrc.setFileName(getFileName());
		}
		catch (Exception e)
		{
			notifyCompletionObservers();
		}
		
	}

	//获取音乐名
	public String getMusicName()
	{
		if(getFileName()==null){
			return null;
		}
		if(getFileName().contains("-")){
			musicName=getFileName().split("-")[1];
		}else{
			musicName=getFileName();
		}
	    
		return musicName;
	}

	//获取歌手名
	public String getSinger()
	{
		if(getFileName()==null){
			return null;
		}
		if(getFileName().contains("-")){
			singer=getFileName().split("-")[0];
		}else{
			singer="未知歌手";
		}
		return singer;
	}
	
	//获取当前歌词
	public String getCurrentLrc(){
		return musicLrc.getMusicLrcContentByTime(getSeekTime());
	}
	
	/**
	 * 定时更新界面类
	 */
	class NotifyThread extends Thread
	{
		@Override
		public void run()
		{
			while(true){
				try
				{
					notifyViewObservers();
					Thread.sleep(500);
				}
				catch (InterruptedException e)
				{}
			}
		}
	}

}
